import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_pagewise/flutter_pagewise.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/mode/mine/mine_msg_bean.dart';
import 'package:loxen/mode/study/study_info_bean.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/format_time.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/mine_msgs_viewmodel.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/widget/my_subassembly.dart';

class MineMsgWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<MineMsgViewModel>(
      create: (_) {
        var model = MineMsgViewModel();
        model.loadData();
        return model;
      },
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size(double.infinity, 95.h),
          child: Container(
            color: Theme.of(context).backgroundColor,
            child: SafeArea(
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      MySubassembly.getBackIcon(context,
                          color: Theme.of(context).textTheme.subtitle2.color),
                      Expanded(
                        child: Center(
                          child: Text(
                            StringResources.tong_zhi_xiao_xi,
                            style: TextStyle(
                                fontSize: 32.sp,
                                fontWeight: FontWeight.w500,
                                color: Theme.of(context).textTheme.subtitle2.color),
                          ),
                        ),
                      ),
                      MySubassembly.getSearchIcon(null, text: Text('')),
                    ],
                  )
                ],
              ),
            ),
          ),
        ),
        body: Selector<MineMsgViewModel, BaseState>(
            builder: (context, state, _) {
              if (state == BaseState.LOADING) {
                return LoadingWidget();
              }
              if (state == BaseState.CONTENT) {
                PagewiseLoadController pageLoadController = PagewiseLoadController<MineMsg>(
                    pageFuture: (page) =>
                        Provider.of<MineMsgViewModel>(context, listen: false).nextData(page),
                    pageSize: 10);
                return Container(
                  color: Theme.of(context).backgroundColor,
                  child: RefreshIndicator(
                      child: PagewiseListView<MineMsg>(
                        pageLoadController: pageLoadController,
                        loadingBuilder: (_) => MySubassembly.getLoadingItem(),
                        noItemsFoundBuilder: (_) => MySubassembly.getNullItem(msg: '无更多消息'),
                        itemBuilder: (context, item, index) {
                          return GestureDetector(
                            onTap: () {
                              _routerToDetail(context, item.landing_ct, item.landing_instance_id);
                              print('跳转到详细的页面');
                              print(item.content);
                              print(item.id);
                              print('类型           ' +
                                  item.content +
                                  '   '
                                      'id' +
                                  item.id.toString());
                            },
                            child: Container(
                                decoration: BoxDecoration(
                                    border: Border(
                                        bottom: BorderSide(color: Color(0xFFEBECF1), width: 0.5))),
                                margin: const EdgeInsets.only(left: 18),
                                padding: const EdgeInsets.only(top: 16, bottom: 16),
                                child: Row(
                                  mainAxisSize: MainAxisSize.max,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Image.network(
                                      item.portrait,
                                      fit: BoxFit.cover,
                                      width: 60,
                                    ),
                                    Expanded(
                                      child: Container(
                                        margin: const EdgeInsets.only(left: 14.5, right: 18),
                                        child: Column(
                                          mainAxisSize: MainAxisSize.max,
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Row(
                                              children: <Widget>[
                                                Expanded(
                                                    child: Text(
                                                  item.name,
                                                  style: TextStyle(
                                                      color: Color(0xFF000000),
                                                      fontSize: 16,
                                                      fontWeight: FontWeight.w500,
                                                      height: 1),
                                                )),
                                                Text(
                                                  FormatTime.readTimestamp(item.add_time),
                                                  style: TextStyle(
                                                    color: Color(0xFF82868E),
                                                    fontSize: 12,
                                                    fontWeight: FontWeight.w400,
                                                  ),
                                                ),
                                              ],
                                            ),
                                            Text(
                                              item.content,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                color: Color(0xFF82868E),
                                                fontSize: 12,
                                                fontWeight: FontWeight.w400,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    )
                                  ],
                                )),
                          );
                        },
                      ),
                      onRefresh: () async {
                        Provider.of<MineMsgViewModel>(context, listen: false).refresh();
                        pageLoadController.reset();
                      }),
                );
              }
              if (state == BaseState.EMPTY) {
                return EmptyWidget();
              }
              if (state == BaseState.FAIL) {
                return FailWidget();
              }
              return Text('');
            },
            selector: (_, model) => model.mState),
      ),
    );
  }

  _routerToDetail(BuildContext context, String type, int instanceId) {
    switch (type) {
      case 'idea-article': // 灵感
        InspirationInfo entity = new InspirationInfo();
        entity.id = instanceId;
        Navigator.of(context).pushNamed(Routes.inspiration_detail, arguments: entity);
        break;

      ///  todo 素材的点赞通知待定
      case 'study-knowledge':
        StudyInfo entity = StudyInfo();
        entity.id = instanceId;
        int category = 0;
        Navigator.of(context).pushNamed(Routes.study_detail,
            arguments: {"detail": entity, "currentCategory": category});
        break;
      case 'study-case':
        StudyInfo entity = StudyInfo();
        entity.id = instanceId;
        int category = 1;
        Navigator.of(context).pushNamed(Routes.study_detail,
            arguments: {"detail": entity, "currentCategory": category});
        break;
    }
  }
}
